package com.xul.controller;

import com.xul.entity.*;
import com.xul.service.*;
import com.xul.utils.MapController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xul
 * @create 2021-07-06 16:57
 */

@Controller
@RequestMapping("section")
public class SectionController {

    @Autowired
    private SectionService sectionService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private ClazzService clazzService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private ScoreService scoreService;

    private final String LIST = "section/list";
    private final String ADD = "section/add";
    private final String UPDATE = "section/update";


    @GetMapping("add")
    public String add(Integer clazzId, ModelMap modelMap) {
        List<Teacher> teachers = teacherService.query(null);
        List<Course> courses = courseService.query(null);
        modelMap.addAttribute("teachers", teachers);
        modelMap.addAttribute("courses", courses);
        modelMap.addAttribute("clazzId", clazzId);
        return ADD;
    }

    @PostMapping("create")
    @ResponseBody
    public Map<String, Object> create(Section section) {
        int result = sectionService.create(section);
        if (result <= 0) {
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }

    @PostMapping("delete/{id}")
    @ResponseBody
    public Map<String, Object> delete(@PathVariable("id") Integer id) {
        int result = sectionService.delete(id);
        if (result <= 0) {
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }

    @PostMapping("delete")
    @ResponseBody
    public Map<String, Object> delete(String ids) {
        int result = sectionService.delete(ids);
        if (result <= 0) {
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }

    @PostMapping("update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody Section section) {
        int result = sectionService.update(section);
        if (result <= 0) {
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }

    @GetMapping("detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap modelMap) {
        Section section = sectionService.detail(id);
        List<Teacher> teachers = teacherService.query(null);
        List<Course> courses = courseService.query(null);
        modelMap.addAttribute("teachers", teachers);
        modelMap.addAttribute("courses", courses);
        modelMap.addAttribute("section", section);
        return UPDATE;
    }


    @PostMapping("query")
    @ResponseBody
    public Map<String, Object> query(@RequestBody Section section) {
        List<Section> list = sectionService.query(section);

        List<Teacher> teachers = teacherService.query(null);
        List<Course> courses = courseService.query(null);

        list.forEach(entity -> {
            teachers.forEach(teacher -> {
                if (entity.getTeacherId() == teacher.getId().intValue()) {
                    entity.setTeacher(teacher);
                }
            });
            courses.forEach(course -> {
                if (entity.getCourseId() == course.getId().intValue()) {
                    entity.setCourse(course);
                }
            });

        });


        Integer count = sectionService.count(section);
        return MapController.getInstance().success().put("data", list).put("count", count).getMap();
    }

    @GetMapping("list")
    public String list() {
        return LIST;
    }

    @PostMapping("tree")
    @ResponseBody
    public List<Map<String, Object>> tree(@RequestBody Section section) {

        List<Clazz> clazzes = clazzService.query(null);
        List<Subject> subjects = subjectService.query(null);

        List<Map<String, Object>> list = new ArrayList<>();

        subjects.forEach(subject -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", subject.getId());
            map.put("name", subject.getSubjectName());
            map.put("parentId", 0);
            List<Map<String, Object>> childrenList = new ArrayList<>();
            clazzes.forEach(clazz -> {
                if (clazz.getSubjectId() == subject.getId().intValue()) {
                    Map<String, Object> children = new HashMap<>();
                    children.put("id", clazz.getId());
                    children.put("name", clazz.getClazzName());
                    children.put("parentId", subject.getId());
                    childrenList.add(children);
                }
            });
            map.put("children", childrenList);
            list.add(map);
        });

        return list;
    }

    @GetMapping("student_section")
    public String student_section() {
        return "section/student_section";
    }

    @PostMapping("query_student_section")
    @ResponseBody
    public Map<String, Object> student_section(HttpSession session) {
        Student student = (Student) session.getAttribute("user");
        List<Section> sections = sectionService.queryByStudent(student.getId());
        List<Clazz> clazzes = clazzService.query(null);
        List<Teacher> teachers = teacherService.query(null);
        List<Course> courses = courseService.query(null);
        sections.forEach(section -> {
            clazzes.forEach(clazz -> {
                if (section.getClazzId() == clazz.getId().intValue()) {
                    section.setClazz(clazz);
                }
            });
            teachers.forEach(teacher -> {
                if (section.getTeacherId() == teacher.getId().intValue()) {
                    section.setTeacher(teacher);
                }
            });
            courses.forEach(course -> {
                if (section.getCourseId() == course.getId().intValue()) {
                    section.setCourse(course);
                }
            });
        });
        return MapController.getInstance().success().add("data", sections).getMap();
    }

    @GetMapping("teacher_section")
    public String teacher_section() {
        return "section/teacher_section";
    }

    @PostMapping("query_teacher_section")
    @ResponseBody
    public Map<String, Object> query_teacher_section(HttpSession session) {
        // 获取老师登录信息
        Teacher teacher = (Teacher) session.getAttribute("user");
        Section section = new Section();
        section.setTeacherId(teacher.getId());
        List<Section> sections = sectionService.query(section);
        List<Clazz> clazzes = clazzService.query(null);
        List<Course> courses = courseService.query(null);

        sections.forEach(section1 -> {
            clazzes.forEach(clazz -> {
                if (section1.getClazzId() == clazz.getId().intValue()) {
                    section1.setClazz(clazz);
                }
            });
            courses.forEach(course -> {
                if (section1.getCourseId() == course.getId().intValue()) {
                    section1.setCourse(course);
                }
            });
        });

        return MapController.getInstance().success().add("data", sections).getMap();
    }

    @GetMapping("teacher_student_score")
    public String teacher_student_score(Integer courseId,Integer sectionId,ModelMap modelMap){
        List<HashMap> list = studentService.querySelectStudent(courseId, sectionId);
        modelMap.put("list",list);
        modelMap.put("courseId",courseId);
        modelMap.put("sectionId",sectionId);
        return "section/teacher_student_score";
    }

    @PostMapping("teacher_student_score")
    public Map<String,Object> teacher_student_score(Integer courseId,Integer sectionId,String stuIds,String scores){
        int result = scoreService.update(courseId,sectionId,stuIds,scores);
        if(result <= 0){
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }

}
